package com.ruoyi.app.controller;

import com.ruoyi.app.interceptor.SessionUtil;
import com.ruoyi.cms.domain.CrmMeetReserve;
import com.ruoyi.cms.domain.CrmMeetRoom;
import com.ruoyi.cms.domain.CrmMember;
import com.ruoyi.cms.service.ICrmMeetReserveService;
import com.ruoyi.cms.service.ICrmMeetRoomService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会议室Controller
 * 
 * @author hct
 * @date 2022-10-06
 */
@RestController
@RequestMapping("/app/meetRoom")
public class AppMeetRoomController extends BaseController
{
    @Autowired
    private ICrmMeetRoomService crmMeetRoomService;
    @Autowired
    private ICrmMeetReserveService crmMeetReserveService;

    /**
     * 查询会议室列表
     */
    @GetMapping("/list")
    public AjaxResult list(CrmMeetRoom crmMeetRoom)
    {
        List<CrmMeetRoom> list = crmMeetRoomService.selectCrmMeetRoomList(crmMeetRoom);
        return AjaxResult.success(list);
    }

    @GetMapping(value = "detail/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(crmMeetRoomService.selectCrmMeetRoomById(id));
    }

    /**
     * 所有预约的会议室
     */
   /* @GetMapping("/reserveMeetList")
    public AjaxResult reserveMeetList(CrmMeetReserve crmMeetReserve)
    {
        List<CrmMeetReserve> list = crmMeetReserveService.selectCrmMeetReserveList(crmMeetReserve);
        return AjaxResult.success(list);
    }
*/
    /**
     * 我预约的会议室
     */
    @GetMapping("/myReserveMeetList")
    public AjaxResult list(CrmMeetReserve crmMeetReserve)
    {
        CrmMember member=SessionUtil.getLoginUser();
        crmMeetReserve.setMemberId(member.getId());
        List<CrmMeetReserve> list = crmMeetReserveService.selectCrmMeetReserveList(crmMeetReserve);
        return AjaxResult.success(list);
    }

    /**
     * 会议室的预定情况列表
     * @param meetId  会议室id
     * @param day YYYY-mm-dd
     * @return
     */
    @GetMapping("/reserveMeetList")
    public AjaxResult reserveMeetId(Long  meetId,String day) {
        // 创建匹配器，进行动态查询匹配
        List<CrmMeetReserve> list=crmMeetReserveService.getList(meetId,day);
        return AjaxResult.success(list);
    }
    /**
     * 新增会议预约
     */
    @PostMapping("reserve")
    public AjaxResult reserve( CrmMeetReserve crmMeetReserve)
    {
        return toAjax(crmMeetReserveService.insertCrmMeetReserve(crmMeetReserve));
    }


    /**
     * 会议室预定
     */
    @PostMapping("/reserve/{meetId}")
    public AjaxResult reserveMeetId(@PathVariable("meetId")Long  meetId,String startTime,String endTime) {
        CrmMeetReserve meetReserve=new CrmMeetReserve();
        CrmMeetRoom meetRoom=new CrmMeetRoom();
        meetRoom.setId(meetId);
        meetReserve.setMeetId(meetId);
        // 复制保留无需修改的数据
        CrmMember memberInfo = SessionUtil.getLoginUser();
        meetReserve.setMemberId(memberInfo.getId());
        meetReserve.setStartTime(DateUtils.parseDate(startTime));
        meetReserve.setEndTime(DateUtils.parseDate(startTime));
        crmMeetReserveService.insertCrmMeetReserve(meetReserve);
        return AjaxResult.success(meetReserve);
    }

    /**
     * 修改会议预约
     */
    @PostMapping("edit")
    public AjaxResult edit( CrmMeetReserve crmMeetReserve)
    {
        return toAjax(crmMeetReserveService.updateCrmMeetReserve(crmMeetReserve));
    }

    @PostMapping("delete/{id}")
    public AjaxResult delete(@PathVariable Long id)
    {
        return toAjax(crmMeetReserveService.deleteCrmMeetReserveById(id));
    }
    @PostMapping("batchDelete/{ids}")
    public AjaxResult batchDelete(@PathVariable Long[] ids)
    {
        return toAjax(crmMeetReserveService.deleteCrmMeetReserveByIds(ids));
    }
}
